<?php
namespace Content\Controller;

use Base\Controller\AdminBaseController;

class CategoryController extends AdminBaseController {
	
	protected $categoryModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->categoryModel = D("Content/Category");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$categoryJson = file_get_contents('php://input');
		$category = json_decode($categoryJson, true);
		
		// create & update
		$category['status'] = 0;
		$category['add_time'] = date("Y-m-d H:i:s");
		$category['update_time'] = date("Y-m-d H:i:s");

		// model
		if ($category['model']) {
			list($category['model_table_name'], $category['model_name']) = split(',', $category['model']);
		}

		// 执行添加
		$result = $this->categoryModel->add($category);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newCategory = $this->categoryModel->where(array("id" => $result))->find();
			
		// 如果前台未设置url，则设置
		if (empty($newCategory['url'])) {
		    $newCategory['url'] = C('CATEGORY_RULE') . $id;
		    $this->categoryModel->where(array("id" => $id))->save($newCategory);
		}
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newCategory;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->categoryModel->order(array("sort_order" => "asc"))->select();

		$data['code'] = 0;
		$data['msg'] = '找到目标！';
		$data['obj'] = $result;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有by model table name  */
    public function getAllByModelTableName() {
    	$modelTableName = $_GET['modelTableName'];
		$result = $this->categoryModel->where(array("model_table_name" => $modelTableName))->select();

		$data['code'] = 0;
		$data['msg'] = '找到目标！';
		$data['obj'] = $result;
		$this->ajaxReturn($data);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$category = $this->categoryModel->where(array("id" => $id))->find();
		if (!$category) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// model
		$category['model'] = $category['model_table_name'] . "," . $category['model_name'];
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $category;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$categoryJson = file_get_contents('php://input');
		$category = json_decode($categoryJson, true);

		// update
		$category['status'] = 0;										// update reset
		$category['update_time'] = date("Y-m-d H:i:s");

		// model
		if ($category['model']) {
			list($category['model_table_name'], $category['model_name']) = split(',', $category['model']);
		} else {
			$category['model_table_name'] = "";
			$category['model_name'] = "";
		}
		
		// 执行更新
		$result = $this->categoryModel->where(array("id" => $id))->save($category);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedCategory = $this->categoryModel->where(array("id" => $id))->find();
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedCategory;
		$this->ajaxReturn($data);
	}

	/** 批量更新缓存（URL） */
	public function batUpdateCache() {
		$result = $this->categoryModel->select();
		foreach ($result as $v) {
			if ($v['type'] != 4) {						// 如果不为外链
				// update
				$v['status'] = 0;										// update reset
				$v['update_time'] = date("Y-m-d H:i:s");
				
				if ($v['is_html']) {					// 静态化用+++
// 					$v['url'] = C('CATEGORY_RULE_STATIC') . $v['dir_name'];
					$v['url'] = $v['dir_name'] . '/index' . C('HTML_FILE_SUFFIX');
				} else {
					$v['url'] = C('CATEGORY_RULE') . $v['id'];
				}
				$this->categoryModel->where(array("id" => $v['id']))->save($v);
			}
		}
		
		$data['code'] = 0;
		$data['msg'] = '批量更新缓存完成！';
		$this->ajaxReturn($data);
	}
	
	/** 排序 */
	public function sortOrder() {
		$result = parent::_sortOrder($this->categoryModel);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '编辑排序失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑排序成功！';
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$category = $this->categoryModel->where(array("id" => $id))->find();
		if (!$category) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		$count = $this->categoryModel->where(array("pid" => $id))->count();
		if ($count > 0) {
			$data['code'] = 499;
			$data['msg'] = '该菜单下还有子类，无法删除！';
			$this->ajaxReturn($data);
		}

		$modelTableName = $category['model_table_name'];
		if ($modelTableName != null && $modelTableName != "") {
			$count = M($modelTableName)->where(array("cid" => $id))->count();
			if ($count > 0) {
				$data['code'] = 499;
				$data['msg'] = '该分类下有内容，无法删除！';
				$this->ajaxReturn($data);
			}
		}
		
		if (!$this->categoryModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':category');
		return;
	}
	
}